﻿using KinonekoSoftware.Foundation.Languages;

namespace KinonekoSoftware.Foundation
{
    public class DialogRule
    {
        private readonly Predicate<object> _expression;
        private readonly string            _reason;

        protected DialogRule(Predicate<object> expression, string reason)
        {
            _expression = expression;
            _reason     = reason;
        }

        public bool Validate(object parameter)
        {
            return _expression(parameter);
        }

        public static DialogRule Create<T>(string reason, Predicate<T> predicate, bool useKey = true)
        {
            ArgumentNullException.ThrowIfNull(predicate);

            //
            //
            var text = useKey ? reason : LanguageManager.GetText(reason);

            //
            //
            return new DialogRule(Expression, text);

            //
            //
            bool Expression(object x) => predicate((T)x);
        }
        

        public string Reason => _reason;
    }
}